import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import * as api from './api';
import { auth } from "/@/utils/authFunction";
import { computed } from 'vue';
import { ElMessage } from 'element-plus';
import { h } from 'vue';
import { ElTag } from 'element-plus';

export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
    const pageRequest = async (query: any) => {
        return await api.GetList(query);
    };
    const editRequest = async ({ form, row }: EditReq) => {
        if (row.id) {
            form.id = row.id;
        }
        return await api.UpdateObj(form);
    };
    const delRequest = async ({ row }: DelReq) => {
        return await api.DelObj(row.id);
    };
    const addRequest = async ({ form }: AddReq) => {
        return await api.AddObj(form);
    };

    const exportRequest = async (query: UserPageQuery) => {
        return await api.exportData(query);
    };

    // 状态字典
    const statusDict = dict({
        data: [
            { value: 0, label: '待审批', type: 'warning' },
            { value: 1, label: '已审批', type: 'success' },
            { value: 2, label: '已拒绝', type: 'danger' },
        ]
    });

    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                editRequest,
                delRequest,
            },
            actionbar: {
                buttons: {
                    export: {
                        show: auth('PurchasePlan:Export'),
                        text: "导出",
                        title: "导出",
                        click() {
                            return exportRequest(crudExpose.getSearchFormData());
                        }
                    },
                    add: {
                        show: auth('PurchasePlan:Create'),
                    },
                }
            },
            rowHandle: {
                fixed: 'right',
                width: 300,
                buttons: {
                    view: {
                        link: true,
                        text: '查看',
                        order: 1,
                        show: auth('PurchasePlan:Retrieve')
                    },
                    edit: {
                        link: true,
                        text: '编辑',
                        order: 2,
                        show: auth('PurchasePlan:Update')
                    },
                    approve: {
                        link: true,
                        text: '审批',
                        order: 3,
                        show: computed(() => auth('PurchasePlan:Approve')),
                        if: (context: any) => context.row.status === 0,
                        click: async (context: any) => {
                            try {
                                await api.approvePlan(context.row.id, 'approve');
                                ElMessage.success('审批成功');
                                crudExpose.doRefresh();
                            } catch (error) {
                                console.error('审批失败:', error);
                                ElMessage.error('审批失败');
                            }
                        }
                    },
                    reject: {
                        link: true,
                        text: '拒绝',
                        order: 4,
                        show: computed(() => auth('PurchasePlan:Approve')),
                        if: (context: any) => context.row.status === 0,
                        click: async (context: any) => {
                            try {
                                await api.approvePlan(context.row.id, 'reject');
                                ElMessage.success('已拒绝');
                                crudExpose.doRefresh();
                            } catch (error) {
                                console.error('操作失败:', error);
                                ElMessage.error('操作失败');
                            }
                        }
                    },
                    remove: {
                        link: true,
                        text: '删除',
                        order: 5,
                        show: auth('PurchasePlan:Delete')
                    },
                },
            },
            columns: {
                title: {
                    title: '计划标题',
                    type: 'input',
                    search: { show: true },
                    column: {
                        minWidth: 160,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '计划标题必填' }],
                        component: {
                            placeholder: '请输入计划标题',
                        },
                    },
                },
                plan_date: {
                    title: '计划日期',
                    type: 'date',
                    search: { show: true },
                    column: {
                        width: 120,
                        component: { name: "fs-date-format", format: "YYYY-MM-DD" }
                    },
                    form: {
                        rules: [{ required: true, message: '计划日期必填' }],
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '请选择计划日期',
                        },
                    },
                },
                total_amount: {
                    title: '总金额',
                    type: 'number',
                    search: { show: false },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '总金额必填' }],
                        component: {
                            placeholder: '请输入总金额',
                            precision: 2,
                            step: 0.01,
                        },
                    },
                },
                status: {
                    title: '审批状态',
                    type: 'dict-select',
                    dict: statusDict,
                    search: { show: true },
                    column: {
                        width: 100,
                        component: {
                            render: ({ row }) => {
                                const item = statusDict.data.find(item => item.value === row.status);
                                return h(ElTag, { type: item?.type }, () => item?.label || '');
                            }
                        }
                    },
                    form: {
                        show: false,
                    },
                },
                remark: {
                    title: '备注',
                    type: 'textarea',
                    search: { show: false },
                    column: {
                        minWidth: 200,
                    },
                    form: {
                        component: {
                            placeholder: '请输入备注信息',
                            rows: 4,
                        },
                    },
                },
            },
        },
    };
} 